package com.zx.mobile.service.proxy;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.zx.map.service.model.EncashLogModel;
import com.zx.mobile.domain.EncashListDaily;
import com.zx.mobile.domain.EncashQueryDomain;
import com.zx.mobile.domain.MobileUserDomain;
import com.zx.platform.utils.DateUtils;
import com.zx.platform.utils.PmpUtils;

public class EncashQueryProxy {

	/**
	 * 模型转换
	 * @param user
	 * @param encashQueryDomain
	 * @return
	 */
	public static EncashQueryDomain transformQueryDomain(MobileUserDomain user,
			EncashQueryDomain encashQueryDomain) {
		if (StringUtils.isBlank(encashQueryDomain.getBeginDate())) {
			encashQueryDomain.setBeginDate(PmpUtils.dateToString(
					DateUtils.getMonthFirstDateOf(new Date()), "yyyyMMdd"));
		}
		if (StringUtils.isBlank(encashQueryDomain.getEndDate())) {
			encashQueryDomain.setEndDate(PmpUtils.dateToString(new Date(),
					"yyyyMMdd"));
		}

		encashQueryDomain.setMerchantId(user.getMerchantInfoDetailModel()
				.getMerchantId());
		return encashQueryDomain;
	}

	/**
	 * 汇总统计
	 * @param ordList
	 * @return
	 */
	public static Map<String, Object> transformOrdList(
			List<EncashLogModel> encashLogModels) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<EncashListDaily> list = new ArrayList<EncashListDaily>();
		Map<String, EncashListDaily> contain = new HashMap<String, EncashListDaily>();
		BigDecimal totalAmt = new BigDecimal("0");
		for (EncashLogModel temp : encashLogModels) {
			String date = PmpUtils.dateToString(temp.getCreateDate(),
					"yyyy/MM/dd");
			if (contain.containsKey(date)) {
				// map中包含数据的 增加数据
				EncashListDaily encashListDaily = contain.get(date);
				encashListDaily.getEncashLogModelList().add(temp);
			} else {
				// map中不包含数据的 构造数据
				EncashListDaily encashListDaily = new EncashListDaily();
				List<EncashLogModel> dateList = new ArrayList<EncashLogModel>();
				encashListDaily.setEncashDate(date);
				dateList.add(temp);
				encashListDaily.setEncashLogModelList(dateList);
				list.add(encashListDaily);
				contain.put(date, encashListDaily);
			}
		}
		Collections.sort(list, new Comparator<EncashListDaily>() {
			public int compare(EncashListDaily o1, EncashListDaily o2) {
				return o2.getEncashDate().compareTo(o1.getEncashDate());
			}
		});
		for (EncashListDaily encashTemp : list) {
			Collections.sort(encashTemp.getEncashLogModelList(),
					new Comparator<EncashLogModel>() {
						public int compare(EncashLogModel o1, EncashLogModel o2) {
							return o2.getCreateDate().compareTo(
									o1.getCreateDate());
						}
					});
		}
		map.put("list", list);
		map.put("totalAmt", totalAmt.doubleValue());
		return map;
	}

}
